﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Support.Constants;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using MS.InformationManagement.Support;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint.Linq;

namespace MS.InformationManagement.BLL.SearchUser
{
    public class SearchUser
    {
        public static int GetIdByCedula(string Cedula)
        {
            int idUser = 0;
            List<UserProfile> userProfileList = MS.InformationManagement.Support.PeopleHelper.GetUserListByProperty(GlobalConstants.UP_CEDULA, Cedula);
            UserProfile UserProfile = userProfileList.FirstOrDefault();
            if (UserProfile != null)
            {
                SPUser user = SPContext.Current.Web.SiteUsers[UserProfile[GlobalConstants.UP_ACCOUNT].ToString()];
                idUser = user.ID;
            }
            return idUser;
        }


        public static List<UserProfile> GetUsersByDepartment(string Departamento, int pageIndex, int pageSize, out int totalRecords)
        {
            int startingPosition = pageIndex * pageSize;
            List<UserProfile> UsersByDepartment = PeopleHelper.GetUserListByProperty(GlobalConstants.UP_DEPARTMENT, Departamento, true);
            totalRecords = UsersByDepartment.Count();
            return UsersByDepartment.OrderBy(x => x.DisplayName).Skip(startingPosition).Take(pageSize).ToList();
        }

        /// <summary>
        /// Método que obtiene los usuarios que pertenecen a una propiedad y poseen un rol especifico
        /// </summary>
        /// <param name="PropertyName"></param>
        /// <param name="PropertyValue"></param>
        /// <param name="includeCurrentUser"></param>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetUsersByPropertyAndRole(string PropertyName, string PropertyValue, bool includeCurrentUser, string RoleName)
        {
            Dictionary<string, string> users = new Dictionary<string, string>();
            SPServiceContext context = SPServiceContext.GetContext(SPContext.Current.Site);
            UserProfileManager profileManager = new UserProfileManager(context);
            foreach (UserProfile currentProfile in profileManager)
            {
                string userName = currentProfile.DisplayName;
                ProfileValueCollectionBase profileValueCollection = currentProfile.GetProfileValueCollection(PropertyName);
                if ((profileValueCollection != null) && (profileValueCollection.Value != null))
                {
                    if (PropertyValue == profileValueCollection.Value.ToString())
                    {
                        string currentPerfil = currentProfile[GlobalConstants.UP_USER_PERFIL].Value.ToString();
                        VincularPerfiles perfil = AllowingAccessControl.AllowingAccessControl.GetPerfil(currentPerfil, SPContext.Current.Web);
                        var allRoles = AllowingAccessControl.AllowingAccessControl.GetAllRoles(currentPerfil, SPContext.Current.Web);
                        var res = (from rolRibbon in allRoles
                                   where rolRibbon.Role == RoleName &&
                                   (from role in perfil.TipoRole.ToList()
                                    select role.Id).Contains(rolRibbon.Id)
                                   select rolRibbon.Role).ToList();
                        if (res.Count > 0)
                            users.Add(currentProfile[GlobalConstants.UP_ACCOUNT].Value.ToString().ToUpper(), currentProfile.DisplayName);
                    }
                }
            }
            if (!includeCurrentUser && users.ContainsKey(SPContext.Current.Web.CurrentUser.LoginName.ToUpper())) { users.Remove(SPContext.Current.Web.CurrentUser.LoginName.ToUpper()); }
            return users;
        }
    }
}
